package com.tony.demo.ui.performance.image;

import android.view.View;
import android.widget.ImageView;

import com.tony.demo.R;
import com.tony.demo.mvp.presenter.impl.BasePresenter;
import com.tony.demo.ui.base.BaseActivity;

import java.io.IOException;
import java.io.InputStream;

public class BigImageActivity extends BaseActivity {

    BigImageView bigImageView;
    ImageView imageView;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_big_image;
    }

    private void loadImage(int loadType) {
        bigImageView = findViewById(R.id.big_image_view);
        imageView = findViewById(R.id.image_view);

        imageView.setVisibility(View.GONE);
        bigImageView.setVisibility(View.VISIBLE);

        InputStream open = null;
        try {
//            open = getAssets().open("welcome_1.png");
//            open = getAssets().open("big_two.png");
            if (loadType == 1) {
                open = getAssets().open("big.png");
//                open = getAssets().open("re_ba.png");
                bigImageView.setImage(open);
            } else if (loadType == 2) {
                open = getAssets().open("big_horizontal.png");
                bigImageView.setImage(open);
            } else if (loadType == 3) {
                open = getAssets().open("re_ba_big.png");
                bigImageView.setImage(open);
            } else if (loadType == 0) {
                //消耗内存过大，加载不出来
                imageView.setVisibility(View.VISIBLE);
                bigImageView.setVisibility(View.GONE);
//                imageView.setImageBitmap(BitmapFactory.decodeStream(open));
                imageView.setImageResource(R.mipmap.re_ba_big);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != open) {
                try {
                    open.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.vertical_scale:  //纵向加载长图
                loadImage(1);
                break;
            case R.id.horizontal_scale:  //横向加载长图
                loadImage(2);
                break;
            case R.id.no_scale:  //不缩放加载大图
                loadImage(3);
                break;
            case R.id.load_source_img: //加载整图
                loadImage(0);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (null != bigImageView) {
            bigImageView.releaseBitmap();
        }
        super.onDestroy();
    }
}
